package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.service.IUserGroupService;
import com.tarena.lbs.common.basic.converter.UserGroupConverter;
import com.tarena.lbs.pojo.basic.bo.UserGroupBO;
import com.tarena.lbs.pojo.basic.param.UserGroupParam;
import com.tarena.lbs.pojo.basic.query.MarketingSelectedQuery;
import com.tarena.lbs.pojo.basic.query.UserGroupQuery;
import com.tarena.lbs.pojo.basic.vo.UserGroupVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "人群管理")
public class UserGroupController {

    @Resource
    private IUserGroupService userGroupService;

    @Resource
    private UserGroupConverter userGroupConverter;

    @PostMapping("/admin/basic/userGroup/list")
    @ApiOperation("获取人群列表-分页")
    public Result<PageResult<UserGroupVO>> getAll(@RequestBody UserGroupQuery query) {
        BasePageList<UserGroupBO> userBos = userGroupService.userList(query);
        PageResult<UserGroupVO> page = userGroupConverter.assemblePageVOS(userBos,query);
        return new Result<>(page);
    }

    @GetMapping("/admin/basic/userGroup/detail/{id}")
    @ApiOperation("用户人群详情")
    public Result<UserGroupVO> userGroupDetail(@PathVariable("id") Integer id) {
        UserGroupBO bo = userGroupService.getDetail(id);
        return new Result<>(userGroupConverter.bo2vo(bo));
    }

    @PostMapping("/admin/basic/userGroup/save")
    @ApiOperation("新增人群")
    public Result<Void> insert(@Validated @RequestBody UserGroupParam param) {
        userGroupService.insert(param);
        return Result.success();
    }

    @PostMapping("admin/basic/userGroup/select/list")
    @ApiOperation("获取活动选中的人群")
    public Result<PageResult<UserGroupVO>> marketingUserGroupList(@RequestBody MarketingSelectedQuery query) throws Exception {
        BasePageList<UserGroupBO> result = userGroupService.marketingUserGroupList(query);
        return new Result<>(userGroupConverter.assemblePageVOS(result,query));
    }
}
